<?php

require_once("DBMySQL.class.php");

function getUsers() {
	$users = array();
	
	try {
		$oDB = new DBMySQL();
		$result = $oDB->query("SELECT * from userlist");
		
		if( $result == false) {
			throw new Exception('getUsers: SELECT query is failed.');
		}
		
		for( $i = 0; $row = $result->fetch_assoc(); $i++ ) {
            $user = array( 'id' => $row['id'],
            			   'user_nm' => $row['user_nm'],
                           'user_email' => $row['user_email'] );
            array_push($users, $user);
        }
		
		$oDB->close();
		
	} catch(Exception $e) {
		error_log('Caught Exception : ' . $e->getMessage() . '\n');
	}
	
	return json_encode( $users );
}

function newUser($user_nm, $user_pw) {
	$ret = array('result' => 'fail');
	
	if(strlen($user_nm) < 4) {
		$ret = array('result' => 'fail', 'type' => 'short_name');
		return json_encode($ret);
	} else if(strlen($user_pw) < 6) {
		$ret = array('result' => 'fail', 'type' => 'short_password');
		return json_encode($ret);
	} 
		
	
	try {
		$oDB = new DBMySQL();
		$result = $oDB->query("SELECT * FROM userlist WHERE user_nm = '$user_nm'");
		
		if( $result == false) {
			throw new Exception('newUser: SELECT query is failed.');
		}
    	$count = mysqli_num_rows($result);
		
		if($count > 0)
		{
			$ret = array('result' => 'fail', 'type' => 'exist_name');
		}
		else {
			$prepare = $oDB->prepare("INSERT INTO userlist (user_nm, user_pw) 
								values (?, ?)");
			if($prepare == false) {
				throw new Exception('newUser: prepare INSERT query is failed.');
			}
					
			$prepare->bind_param('ss',
						$user_nm,
						encryptPW($user_pw));			
			
			$resulti = $prepare->execute();
			
			if( $resulti == false) {
				throw new Exception('newUser: INSERT query is failed.');
			}
			
			$ret = array('result' => 'success');
		}		
		
		$oDB->close();
		$oDB = null;
				
	} catch(Exception $e) {
		error_log('Caught Exception : ' . $e->getMessage() . '\n');
	}
	
	return json_encode($ret);
}

function verifyUser($user_nm, $user_pw) {
	
	$ret = array('result' => 'fail');
	
	try {
		$oDB = new DBMySQL();
		$result = $oDB->query("SELECT * FROM userlist WHERE user_nm = '$user_nm'");
		
		if( $result == false) {
			throw new Exception('verifyUser: SELECT query is failed.');
		}
    	$count = mysqli_num_rows($result);
		
		if($count == 1)
		{
			$ret = array('result' => 'success');
		}
		
		$oDB->close();
		$oDB = null;
				
	} catch(Exception $e) {
		error_log('Caught Exception : ' . $e->getMessage() . '\n');
	}
	
	return json_encode($ret);
	
}

function verifyUserPHP($user_nm, $user_pw) {
	
	$ret = array('result' => 'fail');
	
	try {
		$user_pw_enc =  encryptPW($user_pw);
		$oDB = new DBMySQL();
		//echo $user_pw . "   " . $user_pw_enc;
		$result = $oDB->query("SELECT id FROM userlist WHERE user_nm = '$user_nm' AND user_pw = '$user_pw_enc'");
		
		if( $result == false) {
			throw new Exception('verifyUser: SELECT query is failed.');
		}
    	$count = mysqli_num_rows($result);
		if($count == 1)
		{
			$row = $result->fetch_assoc();
			$ret = array('result' => 'success', 'id' => $row['id']);
			//echo $result['id'];
		}
		
		$oDB->close();
		$oDB = null;
		
	} catch(Exception $e) {
		error_log('Caught Exception : ' . $e->getMessage() . '\n');
	}
	return $ret;
	
}

function encryptPW($pw) {
	return md5('lifequest'.$pw);
}


?>